package net.hands.common;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
 * 
* 说明:编码解码器
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class Base64 {
   /**
    *字符表
    */
	private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
			.toCharArray();
	/**
	 * 字符容器
	 */
	private static final int[] INV = new int[256];

	static {
		Arrays.fill(INV, -1);
		for (int i = 0, iS = CHARS.length; i < iS; i++) {
			INV[CHARS[i]] = i;
		}
		INV['='] = 0;
	}
   /**
   * 说明:获得字符表
   * @return char[]
   * @update 2012-9-24 上午9:52:54
    */
	public static char[] getAlphabet() {
		return CHARS.clone();
	}

	// ---------------------------------------------------------------- char[]
   /**
   * 说明:字符串编码后转字符数组 采用utf-8编码
   * @param s 字符
   * @return char[]
   * @update 2012-9-24 上午9:54:02
    */
	public static char[] encodeToChar(final String s) {
		try {
			return encodeToChar(s.getBytes("utf-8"), false);
		} catch (final UnsupportedEncodingException ignore) {
			return null;
		}
	}
   /**
   *说明:数组编码后转字符数组  采用utf-8编码
   * @param arr 字节数组
   * @return char[]
   * @update 2012-9-24 上午9:55:03
    */
	public static char[] encodeToChar(final byte[] arr) {
		return encodeToChar(arr, false);
	}
   /**
    * 说明:字节数组编码后转字符数组
   * @param arr 字节数组
   * @param lineSeparator 是否分行
   * @return char[] 字节数组
   * @update 2012-9-24 上午9:55:45
    */
	public static char[] encodeToChar(final byte[] arr, final boolean lineSeparator) {
		final int len = arr != null ? arr.length : 0;
		if (len == 0) {
			return new char[0];
		}

		final int evenlen = (len / 3) * 3;
		final int cnt = ((len - 1) / 3 + 1) << 2;
		final int destLen = cnt + (lineSeparator ? (cnt - 1) / 76 << 1 : 0);
		final char[] dest = new char[destLen];

		for (int s = 0, d = 0, cc = 0; s < evenlen;) {
			final int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);

			dest[d++] = CHARS[(i >>> 18) & 0x3f];
			dest[d++] = CHARS[(i >>> 12) & 0x3f];
			dest[d++] = CHARS[(i >>> 6) & 0x3f];
			dest[d++] = CHARS[i & 0x3f];

			if (lineSeparator && (++cc == 19) && (d < (destLen - 2))) {
				dest[d++] = '\r';
				dest[d++] = '\n';
				cc = 0;
			}
		}

		final int left = len - evenlen; // 0 - 2.
		if (left > 0) {
			final int i = ((arr[evenlen] & 0xff) << 10)
					| (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);

			dest[destLen - 4] = CHARS[i >> 12];
			dest[destLen - 3] = CHARS[(i >>> 6) & 0x3f];
			dest[destLen - 2] = left == 2 ? CHARS[i & 0x3f] : '=';
			dest[destLen - 1] = '=';
		}
		return dest;
	}
   /**
   * 说明:字符数组解码
   * @param arr 字符数组
   * @return byte[]
   * @update 2012-9-24 上午9:56:47
    */
	public byte[] decode(final char[] arr) {
		final int length = arr.length;
		if (length == 0) {
			return new byte[0];
		}

		int sndx = 0;
		final int endx = length - 1;
		final int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0;
		final int cnt = endx - sndx + 1;
		final int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0;
		final int len = ((cnt - sepCnt) * 6 >> 3) - pad;
		final byte[] dest = new byte[len];

		int d = 0;
		for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
			final int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6
					| INV[arr[sndx++]];

			dest[d++] = (byte) (i >> 16);
			dest[d++] = (byte) (i >> 8);
			dest[d++] = (byte) i;

			if (sepCnt > 0 && ++cc == 19) {
				sndx += 2;
				cc = 0;
			}
		}

		if (d < len) {
			int i = 0;
			for (int j = 0; sndx <= endx - pad; j++) {
				i |= INV[arr[sndx++]] << (18 - j * 6);
			}
			for (int r = 16; d < len; r -= 8) {
				dest[d++] = (byte) (i >> r);
			}
		}

		return dest;
	}

	// ---------------------------------------------------------------- byte
   /**
   * 说明:字符编码后转字节数组
   * @param s 字符串
   * @return byte[]
   * @update 2012-9-24 上午9:58:05
    */
	public static byte[] encodeToByte(final String s) {
		try {
			return encodeToByte(s.getBytes("utf-8"), false);
		} catch (final UnsupportedEncodingException ignore) {
			return null;
		}
	}
   /**
   * 说明:字节数组编码后转字节数组
   * @param arr 字节数组
   * @return byte[]
   * @update 2012-9-24 上午9:58:26
    */
	public static byte[] encodeToByte(final byte[] arr) {
		return encodeToByte(arr, false);
	}
   /**
   * 说明:字节转encode编码字节
   * @param arr 字节数组
   * @param lineSep 是否分行
   * @return  byte[]
   * @update 2012-9-24 上午10:02:27
    */
	public static byte[] encodeToByte(final byte[] arr, final boolean lineSep) {
		final int len = arr != null ? arr.length : 0;
		if (len == 0) {
			return new byte[0];
		}

		final int evenlen = (len / 3) * 3;
		final int cnt = ((len - 1) / 3 + 1) << 2;
		final int destlen = cnt + (lineSep ? (cnt - 1) / 76 << 1 : 0);
		final byte[] dest = new byte[destlen];

		for (int s = 0, d = 0, cc = 0; s < evenlen;) {
			final int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);

			dest[d++] = (byte) CHARS[(i >>> 18) & 0x3f];
			dest[d++] = (byte) CHARS[(i >>> 12) & 0x3f];
			dest[d++] = (byte) CHARS[(i >>> 6) & 0x3f];
			dest[d++] = (byte) CHARS[i & 0x3f];

			if (lineSep && ++cc == 19 && d < destlen - 2) {
				dest[d++] = '\r';
				dest[d++] = '\n';
				cc = 0;
			}
		}

		final int left = len - evenlen;
		if (left > 0) {
			final int i = ((arr[evenlen] & 0xff) << 10)
					| (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);

			dest[destlen - 4] = (byte) CHARS[i >> 12];
			dest[destlen - 3] = (byte) CHARS[(i >>> 6) & 0x3f];
			dest[destlen - 2] = left == 2 ? (byte) CHARS[i & 0x3f] : (byte) '=';
			dest[destlen - 1] = '=';
		}
		return dest;
	}
   /**
   * 说明:字节数组解码
   * @param arr 字节数组
   * @return byte[]
   * @update 2012-9-24 上午10:00:05
    */
	public static byte[] decode(final byte[] arr) {
		final int length = arr.length;
		if (length == 0) {
			return new byte[0];
		}

		int sndx = 0;
		final int endx = length - 1;
		final int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0;
		final int cnt = endx - sndx + 1;
		final int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0;
		final int len = ((cnt - sepCnt) * 6 >> 3) - pad;
		final byte[] dest = new byte[len];

		int d = 0;
		for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
			final int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6
					| INV[arr[sndx++]];

			dest[d++] = (byte) (i >> 16);
			dest[d++] = (byte) (i >> 8);
			dest[d++] = (byte) i;

			if (sepCnt > 0 && ++cc == 19) {
				sndx += 2;
				cc = 0;
			}
		}

		if (d < len) {
			int i = 0;
			for (int j = 0; sndx <= endx - pad; j++) {
				i |= INV[arr[sndx++]] << (18 - j * 6);
			}
			for (int r = 16; d < len; r -= 8) {
				dest[d++] = (byte) (i >> r);
			}
		}

		return dest;
	}

	// ---------------------------------------------------------------- string
   /**
   * 说明:字符转编码后字符
   * @param s 字符串
   * @return String 
   * @update 2012-9-24 上午10:00:46
    */
	public static String encodeToString(final String s) {
		try {
			return new String(encodeToChar(s.getBytes("utf-8"), false));
		} catch (final UnsupportedEncodingException ignore) {
			return null;
		}
	}
   /**
   * 说明:字符串解码
   * @param s 字符串
   * @return String
   * @update 2012-9-24 上午10:04:28
    */
	public static String decodeToString(final String s) {
		try {
			return new String(decode(s), "utf-8");
		} catch (final UnsupportedEncodingException ignore) {
			return null;
		}
	}
   /**
   * 说明:字节数组编码后转字符
   * @param arr 字节数组
   * @return String
   * @update 2012-9-24 上午10:04:49
    */
	public static String encodeToString(final byte[] arr) {
		return new String(encodeToChar(arr, false));
	}
   /**
   * 说明:字符编码
   * @param arr 字节数组
   * @param lineSep 分行
   * @return String
   * @update 2012-9-24 上午10:05:18
    */
	public static String encodeToString(final byte[] arr, final boolean lineSep) {
		return new String(encodeToChar(arr, lineSep));
	}
   /**
   * 说明:字符串解码为字节数组
   * @param s 字符串
   * @return byte[] 字节数组
   * @update 2012-9-24 上午10:05:38
    */
	public static byte[] decode(final String s) {
		final int length = s.length();
		if (length == 0) {
			return new byte[0];
		}

		int sndx = 0;
		final int endx = length - 1;
		final int pad = s.charAt(endx) == '=' ? (s.charAt(endx - 1) == '=' ? 2 : 1) : 0;
		final int cnt = endx - sndx + 1;
		final int sepCnt = length > 76 ? (s.charAt(76) == '\r' ? cnt / 78 : 0) << 1 : 0;
		final int len = ((cnt - sepCnt) * 6 >> 3) - pad;
		final byte[] dest = new byte[len];

		int d = 0;
		for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
			final int i = INV[s.charAt(sndx++)] << 18 | INV[s.charAt(sndx++)] << 12
					| INV[s.charAt(sndx++)] << 6 | INV[s.charAt(sndx++)];

			dest[d++] = (byte) (i >> 16);
			dest[d++] = (byte) (i >> 8);
			dest[d++] = (byte) i;

			if (sepCnt > 0 && ++cc == 19) {
				sndx += 2;
				cc = 0;
			}
		}

		if (d < len) {
			int i = 0;
			for (int j = 0; sndx <= endx - pad; j++) {
				i |= INV[s.charAt(sndx++)] << (18 - j * 6);
			}
			for (int r = 16; d < len; r -= 8) {
				dest[d++] = (byte) (i >> r);
			}
		}

		return dest;
	}
}
